var pageSize = 10,pageNum = 1,canScroll=false;

(function(){
  // var name = GetUrlString("name") || "";
  // $("#houseCDN").find(".search").val(name);
  // var data = $("#houseCDN").serialize();
  newList();
})();

// 获取新房列表
function newList(data){
  var pageNum = 1;
  var searchData = data;
  var baseData = "&city="+localStorage.getItem("cityName") +
  "&hpWrite_token="+localStorage.getItem("hpWrite_token")+
  "&pageSize="+pageSize +"&pageNum="+pageNum+'&';
  baseData = searchData ? baseData+searchData : baseData;
  listCount(baseData);
  $.ajax({
      type:'POST',
      url:address+'/newBuildings/list.do?format=true',
      data:baseData,
      dataType:'json',
      success:function(result){
        if(result.status ==200 && result.data.list !=""){
          $("#newList").html(newlistTPL(result.data));
          localStorage.setItem("houseListData",JSON.stringify(result.data));
          canScroll =true;
        }else{
          canScroll =false;
          var html = '<div class="nodata">没有数据</div>';
          $("#newList").html(html);
        }
      },
      error:function(result){
        message(result.message)
      }
  })
};


//滚动加载
$(window).scroll(function(){
    var scrollT = document.documentElement.scrollTop || document.body.scrollTop,
    clientH = $(window).height(),
    docH = $(document).height();
    var $loading = $(".loading");
    if(scrollT >= (docH - clientH) && canScroll==true){
      canScroll = false;
      pageNum ++;
      $loading.show();
      //加载数据
      var data = $("#houseCDN").serialize();
      var baseData = "&city="+localStorage.getItem("cityName") +
      "&hpWrite_token="+localStorage.getItem("hpWrite_token")+
      "&pageSize="+pageSize +"&pageNum="+pageNum+'&';
      data = data + baseData;
      $.ajax({
          type:'POST',
          url:address+'/newBuildings/list.do?format=true',
          data:data,
          dataType:'json',
          success:function(result){
            if(result.status ==200 && result.data.list !=""){
              $("#newList").append(newlistTPL(result.data));
              HouseList = JSON.stringify(result.data) + localStorage.getItem("houseListData");
              localStorage.setItem("houseListData",HouseList);
              canScroll =true;
            }else{
              canScroll =false;
            }
            $(".loading").hide();
          },
          error:function(result){
            message(result.message);
            $(".loading").hide();
          }
      })

    }else{
      $loading.hide();
    }
})


//点击分类查询
$(".menu").on("click","li",function(){
  var $this = $(this),index = $this.index();
  $this.find(".icon-down").addClass("checked");
  $this.siblings().find(".icon-down").removeClass("checked");
  $(".menu-item ul").eq(index).show().siblings().hide();
  $(".coverDig,.menu-item").show();
});
$(".menu-item").on("click","li",function(){
    var $this = $(this),val = $this.attr("data"),index = $this.parent().index();
    $("#classifyInput").find("input").eq(index).val(val);
    var data = $("#houseCDN").serialize();
    newList(data);
    $this.addClass("orange").siblings().removeClass("orange");
    $(".menu").find(".icon-down").removeClass("checked");
    $(".menu-item").hide();
    $(".coverDig").hide();
});

$("#houseCDN").on("keyup",".search",function(){
  var data = $("#houseCDN").serialize();
  newList(data);
});

// 获取总条数
function listCount(data){
  $.ajax({
      type:'POST',
      url:address+'/newBuildings/count.do',
      data:data,
      dataType:'json',
      success:function(result){
        if(result.status ==200){
          $("#listCount").html('共'+result.data.total+'个楼盘');
        }
      }
  })
}

//获取分类 价格
~function classifyPrice(){
  $.ajax({
      type:'POST',
      url:address+'/housepropertyDataDictionary/listNewBuindingsPrice.do',
      data:{},
      dataType:'json',
      success:function(result){
        if(result.status ==200){
          var html ='<li data="0">不限</li>';
          $.each(result.data.list,function(index,info){
            html += '<li data="'+info.key+'">'+info.value+'</li>'
          })
          $("#classifyPrice").html(html);
        }
      }
  })
}();
//获取分类 户型
~function classifyHouseType(){
  $.ajax({
      type:'POST',
      url:address+'/housepropertyDataDictionary/listNewBuindingsType.do',
      data:{},
      dataType:'json',
      success:function(result){
        if(result.status ==200){
          var html ='<li data="0">不限</li>';
          $.each(result.data.list,function(index,info){
            html += '<li data="'+info.key+'">'+info.value+'</li>'
          })
          $("#classifyHouseType").html(html);
        }
      }
  })
}();
//获取分类 开盘时间
~function classifyDate(){
  $.ajax({
      type:'POST',
      url:address+'/housepropertyDataDictionary/listNewBuindingsDate.do',
      data:{},
      dataType:'json',
      success:function(result){
        if(result.status ==200){
          var html ="";
          $.each(result.data.list,function(index,info){
            html += '<li data="'+info.key+'">'+info.value+'</li>'
          })
          $("#classifyDate").html(html);
        }
      }
  })
}();
//获取分类 装修状况
~function classifyDecoration(){
  $.ajax({
      type:'POST',
      url:address+'/housepropertyDataDictionary/listNewBuindingsdecoration.do',
      data:{},
      dataType:'json',
      success:function(result){
        if(result.status ==200){
          var html ='<li data="0">不限</li>';
          $.each(result.data.list,function(index,info){
            html += '<li data="'+info.key+'">'+info.value+'</li>'
          })
          $("#classifyDecoration").html(html);
        }
      }
  })
}();

$(".titleTab span").click(function(){
  $(this).addClass("checked").siblings().removeClass("checked");
});